using System;
using System.Collections.Generic;
using System.Windows.Forms;

using Insesoft.Entities;
using InseSoft.UIControllers;

namespace Insesoft.UI.Procesos
{
    public partial class CotSearchForm : BaseForm
    {
        #region Constructor

        public CotSearchForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Declarations

        private readonly CotizacionController controller = new CotizacionController();

        #endregion
        
        #region Properties

        public Cotizacion SearchResult { get; private set; }

        #endregion
        
        #region Event Handlers

        private void CotSearchFormLoad(object sender, EventArgs e)
        {
            ConfigureToolBar(toolStrip, ToolBarButtons.New |
                                        ToolBarButtons.Search |
                                        ToolBarButtons.Close);
            PopulateControls();
            Initialize();
        }

        private void ToolStripItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch ((ToolBarButtons)e.ClickedItem.Tag)
            {
                case ToolBarButtons.New:
                    Initialize();
                    break;
                case ToolBarButtons.Search:
                    LoadList();
                    break;
                case ToolBarButtons.Close:
                    Close();
                    break;
            }
        }

        private void ChkFechaCheckedChanged(object sender, EventArgs e)
        {
            if (!chkFecha.Checked)
            {
                dtpFecha1.Value = DateTime.Now;
                dtpFecha2.Value = DateTime.Now;
            }
            gbFecha.Enabled = chkFecha.Checked;
        }

        private void GridCellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SearchResult = grid.SelectedRows[0].DataBoundItem as Cotizacion;
            DialogResult = DialogResult.OK;
            Close();
        }

        #endregion

        #region Methods

        private void PopulateControls()
        {
            PopulateComboBox(clienteComboBox, controller.GetAll<Cliente>());
            PopulateComboBox(vendedorComboBox, controller.GetAll<Vendedor>());
            PopulateComboBox(materialComboBox, controller.GetAll<Material>());
            PopulateComboBox(lineaComboBox, controller.GetAll<Linea>());
        }

        private void Initialize()
        {
            ClearControls(Controls);
            chkFecha.Checked = false;
            gbFecha.Enabled = false;
            DisplayList();
            clienteComboBox.Focus();
        }

        private void LoadList()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                DateTime? fechaInicial = null;
                DateTime? fechaFinal = null;
                if (chkFecha.Checked)
                {
                    fechaInicial = dtpFecha1.Value;
                    fechaFinal = dtpFecha2.Value;
                }
                var searchCriteria = new SearchCriteria
                                         {
                                             Cliente = clienteComboBox.SelectedItem as Cliente,
                                             Vendedor = vendedorComboBox.SelectedItem as Vendedor,
                                             Material = materialComboBox.SelectedItem as Material,
                                             Linea = lineaComboBox.SelectedItem as Linea,
                                             FechaInicial = fechaInicial,
                                             FechaFinal = fechaFinal
                                         };
                var list = controller.Search(searchCriteria);
                DisplayList(list);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void DisplayList()
        {
            DisplayList(new List<Cotizacion>());
        }

        private void DisplayList(IList<Cotizacion> cotizaciones)
        {
            grid.DataSource = cotizaciones;
            foreach (DataGridViewColumn col in grid.Columns)
            {
                switch (col.Name)
                {
                    case "Id":
                    case "Linea":
                    case "Fecha":
                    case "Cliente":
                    case "Cantidad":
                        col.Visible = true;
                        break;
                    default:
                        col.Visible = false;
                        break;
                }
            }
            // Anchos de columnas            
            grid.Columns["Id"].Width = 75;
            grid.Columns["Linea"].Width = 175;
            grid.Columns["Fecha"].Width = 85;
            grid.Columns["Cliente"].Width = 200;
            grid.Columns["Cantidad"].Width = 100;
            ConfigureGrid(grid, "Id");
        }

        #endregion
    }
}